<template>
  <el-dialog :title="i18n['user.dialog.update-password']"
             :visible.sync="visible"
             width="25%"
             :append-to-body="true">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item :label="i18n['user.field.old-password.title']"
                    prop="currentPassword">
        <el-input type="password"
                  maxlength="20"
                  v-model="dataForm.currentPassword"></el-input>
      </el-form-item>
      <el-form-item :label="i18n['user.field.new-password.title']"
                    prop="newPassword">
        <el-input type="password"
                  maxlength="20"
                  v-model="dataForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item :label="i18n['user.field.confirm-password.title']"
                    prop="confirmPassword">
        <el-input type="password"
                  maxlength="20"
                  v-model="dataForm.confirmPassword"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">{{i18n['gobal.button.cancel']}}</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">{{i18n['gobal.button.ok']}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { jumpToLogin, encryptPassword } from '@/utils'
import { isLoginPassword } from '@/utils/validate'
export default {
  inject: ['i18n'],
  data () {
    var validateLoginPassword = (rule, value, callback) => {
      if (value && !isLoginPassword(value)) {
        callback(new Error(i18n['user.valid.password.incorrect']))
      } else {
        callback()
      }
    }
    var validateConfirmPassword = (rule, value, callback) => {
      if (this.dataForm.newPassword !== value) {
        callback(new Error(i18n['user.valid.password.inconformity']))
      } else {
        callback()
      }
    }
    return {
      i18n: '',
      visible: false,
      dataForm: {
        userId: 0,
        currentPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      dataRule: {
        currentPassword: [
          { required: true, message: i18n['user.valid.password.not-null'], trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: i18n['user.valid.password.not-null'], trigger: 'blur' },
          { validator: validateLoginPassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: i18n['user.valid.password.not-null'], trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' },
          { validator: validateLoginPassword, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    userName: {
      get () { return this.$store.state.user.name }
    },
    mainTabs: {
      get () { return this.$store.state.common.mainTabs },
      set (val) { this.$store.commit('common/updateMainTabs', val) }
    }
  },
  methods: {
    // 初始化
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/user/updateSelfPassword'),
            method: 'post',
            data: this.$http.adornData({
              // 'userId': this.dataForm.userId,
              'currentPassword': encryptPassword(this.dataForm.currentPassword),
              'newPassword': encryptPassword(this.dataForm.newPassword),
              'confirmPassword': encryptPassword(this.dataForm.confirmPassword)
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: i18n['gobal.hint.succeeded'],
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$nextTick(() => {
                    this.mainTabs = []
                    jumpToLogin()
                    // clearLoginInfo()
                    // this.$router.replace({ name: 'login' })
                  })
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
